#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
ll k;
struct node                         //树节点定义
{
    node *left;
    node *right;
    ll num;
};

node* build(node* now)              //建立树
{
    ll temp;
    cin >> temp;
    if (temp == 0)
        return NULL;
    else
    {
        now->num = temp;
        now->left = build(new node);
        now->right = build(new node);
    }
    return now;
}
void dfs(ll deep, node* now)        //遍历
{
    if (deep == k)
    {
        cout << now->num;
        exit(0);                    //正常运行程序并退出程序 exit(1)为非正常
    }
    if (now->right != NULL)
        dfs(deep + 1, now->right);
    if (now->left != NULL)
        dfs(deep + 1, now->left);
}
int main()
{
    node* root = build(new node);
    cin >> k;
    dfs(0, root);
    cout << "0";
    return 0;
}